package uutalk.netcloud.talk.base.mqtt.callback;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttToken;

import uutalk.netcloud.talk.base.mqtt.MqttConstant;
import uutalk.netcloud.talk.base.mqtt.event.MQTTActionEvent;
import uutalk.netcloud.talk.base.util.thirdpart.EventMamager;

/**
 * Created by hujie on 17/3/30.
 */

public class SimpleMqttActionListener implements IMqttActionListener {
    @Override
    public void onSuccess(IMqttToken asyncActionToken) {
        if (MqttConstant.MQTTUserContextConstant.CONNECT_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.CONNECT_SUCCESS, asyncActionToken, null));
        }
        if (MqttConstant.MQTTUserContextConstant.SUBSCRIBE_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.SUBSCRIBE_SUCCESS, asyncActionToken, null));
        }
        if (MqttConstant.MQTTUserContextConstant.PUBLISH_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.PUBLISH_SUCCESS, asyncActionToken, null));
        }

    }

    @Override
    public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
        if (MqttConstant.MQTTUserContextConstant.CONNECT_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.CONNECT_FAIL, asyncActionToken, exception));
        }
        if (MqttConstant.MQTTUserContextConstant.SUBSCRIBE_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.SUBSCRIBE_FAIL, asyncActionToken, null));
        }
        if (MqttConstant.MQTTUserContextConstant.PUBLISH_CONTEXT.equals(asyncActionToken.getUserContext())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.PUBLISH_FAIL, asyncActionToken, null));
        }
        if (MqttConstant.MQTTUserContextConstant.UNSUBSCRIBE_CONTEXT.equals(asyncActionToken.getActionCallback())) {
            EventMamager.getInstance().postEvent(new MQTTActionEvent(MqttConstant.MQTTStatusConstant.UNSUBSCRIBE_SUCCESS, asyncActionToken, null));
        }
    }
}
